package com.qiaofang.collectmanagebff.facade.impl;

import com.qiaofang.collectmanage.stub.bean.SearchHouseAgentPageDTO;
import com.qiaofang.collectmanage.stub.bean.SearchHouseAgentPostDTO;
import com.qiaofang.collectmanage.stub.bean.SearchOperatorLogPageAndHouseAgentCountDTO;
import com.qiaofang.collectmanage.stub.bean.SearchOperatorLogPostDTO;
import com.qiaofang.collectmanage.stub.constants.CollectRuleResponseCode;
import com.qiaofang.collectmanagebff.facade.request.SearchOperatorLogPageBffRequest;
import com.qiaofang.collectmanagebff.facade.response.SearchOperatorLogPageAndHouseAgentCountBffResponse;
import com.qiaofang.collectmanagebff.service.HouseAgentService;
import com.qiaofang.collectmanagebff.service.OperatorLogService;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.model.page.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/operatorlog")
@Api(value = "操作日志集合", tags = "operatorlog")
public class OperatorLogFacadeImplBff {
    @Autowired
    private OperatorLogService operatorLogService;
    @Autowired
    private HouseAgentService houseAgentService;

    @RequestMapping(value = "/searchOperatorLogPageAndHouseAgentCount", method = RequestMethod.POST)
    @ApiOperation(value = "查询多个对象不包含删的分页列表和中介号码总数", notes = "获取多个对象不包含删的分页列表和中介号码总数")
    @ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "SearchOperatorLogPageBffRequest")})
    public SearchOperatorLogPageAndHouseAgentCountBffResponse searchOperatorLogPageAndHouseAgentCount(@RequestBody @Valid SearchOperatorLogPageBffRequest request, BindingResult bindingResult) {
        SearchOperatorLogPageAndHouseAgentCountBffResponse response = new SearchOperatorLogPageAndHouseAgentCountBffResponse();
        SearchOperatorLogPageAndHouseAgentCountDTO searchOperatorLogPageAndHouseAgentCountDTO = new SearchOperatorLogPageAndHouseAgentCountDTO();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
        PageDTO pageDTO = new PageDTO();
        SearchOperatorLogPostDTO searchOperatorLogPostDTO = new SearchOperatorLogPostDTO();
        pageDTO.setPageSize(request.getPageSize());
        pageDTO.setPageNum(request.getPageNum());
        searchOperatorLogPostDTO.setOperatorType("HOUSEAGENT");
        //获取中介号码有效总数
        SearchHouseAgentPageDTO searchHouseAgentPageDTO = houseAgentService.searchHouseAgentPage(new PageDTO(),new SearchHouseAgentPostDTO());
        searchOperatorLogPageAndHouseAgentCountDTO.setHouseAgentCount(searchHouseAgentPageDTO.getCount());
        searchOperatorLogPageAndHouseAgentCountDTO.setSearchOperatorLogPageDTO(operatorLogService.searchOperatorLogPage(pageDTO, searchOperatorLogPostDTO));
        response.setData(searchOperatorLogPageAndHouseAgentCountDTO);
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }
}

